#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json
from handle.handler import BasicHandler
from analyze.xpath_analyzer import XpathAnalyzer
from store.sub.cyzone import CyzoneProjectListStore, CyzoneProjectStore

import logging
import traceback


class CyzoneProjectListHandler(BasicHandler):
    def __init__(self, config):
        super(CyzoneProjectListHandler, self).__init__(config)
        self.mdb = config['mdb']
        self.mdb_excute_num = 0

    def get_analyzer(self):
        return XpathAnalyzer(self.config)

    def get_store(self):
        return CyzoneProjectListStore(self.config)

    def duplicated_key(self, task):
        # 首页不去重，需要它生成任务
        return 'duplicated:{}:{}'.format(self.name, task['page']) if task['page'] != 1 else None

    def save(self, result, task):
        try:
            projects = result.get('projects', [])
            for project in projects:
                tags = project.get('tags', [])
                project['tags'] = ','.join(e for e in tags)
                self.store.set(self.store.table, project)
                self.mdb_excute_num += 1
            if self.mdb_excute_num > 0:
                self.mdb.commit()
            self.gen_next(result, task)
        except Exception as e:
            logging.error(e)
            logging.error(traceback.format_exc())

    def gen_next(self, result, task):
        try:
            if task['page'] != 1:
                return
            max_range = result.get('max_page', 1) + 1
            for i in range(2, max_range):
                next_task = {
                    'type': task['type'],
                    'url': task['url_format'].format(i),
                    'url_format': task['url_format'],
                    'page': i,
                }
                length = self.mq.push(self.queue_name, json.dumps(next_task))
                logging.info('gen next task {0} {1}'.format(length, next_task))
        except Exception as e:
            logging.error(e)
            logging.error(traceback.format_exc())


class CyzoneProjectHandler(BasicHandler):
    def __init__(self, config):
        super(CyzoneProjectHandler, self).__init__(config)
        self.mdb = config['mdb']

    def get_analyzer(self):
        return XpathAnalyzer(self.config)

    def get_store(self):
        return CyzoneProjectStore(self.config)

    def duplicated_key(self, task):
        return 'duplicated:{}:{}'.format(self.name, task['project_id'])

    def save(self, result, task):
        try:
            result['stage'] = task['stage']
            result['project_id'] = task['project_id']     # 缺少字段 自己添加的字段（周帅）
            tags = result.get('tags', [])
            result['tags'] = ','.join(e for e in tags)
            self.store.set(self.store.table, result)
            self.mdb.commit()
        except Exception as e:
            logging.error(e)
            logging.error(traceback.format_exc())
